/*********************************************************************************
 *      Copyright:  (C) 2023 Lin XinCheng
 *                  All rights reserved.
 *
 *       Filename:  spi_app.c
 *    Description:  This file for spi app.c
 *                 
 *        Version:  1.0.0(2023年07月25日)
 *         Author:  Lin XinCheng <1481155734@qq.com>
 *      ChangeLog:  1, Release initial version on "2023年07月25日 20时48分08秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#define W_WIDTH	128 //oled显示屏列数
#define Y_WIDTH	64 //oled显示屏行数
#define DEVNAME_LEN	64 //文件名长度

#define PLATDRV_MAGIC	0x16 //魔术字
#define OLED_ON			_IO(PLATDRV_MAGIC, 0x18)
unsigned char *buf;
unsigned char rx_buf[1024];

unsigned char databuf[1024] = 
{
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x80,0x80,0x80,
	0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
	0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xC0,
	0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x0F,0x1F,0x3F,0x7E,0xFC,0xF8,0xF0,0xE0,0xC0,
	0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x1E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,
	0x0E,0x0E,0x0E,0x0E,0x0E,0xFF,0xFF,0xFF,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,
	0x0E,0x0E,0x0E,0x0E,0x1E,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x0F,0x0F,
	0x07,0x06,0x3C,0x3C,0x3C,0x3C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x9C,0xDC,0xFC,0xFC,
	0xFC,0xDC,0x9C,0x1C,0x1C,0x1F,0x1F,0x1F,0x1C,0x9C,0xDC,0xFC,0xFC,0xFC,0xDC,0xDC,
	0x9C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x3C,0x3C,0x3C,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xF8,0xFC,
	0xF8,0x70,0x00,0xC0,0xE0,0xE0,0xF0,0xF8,0xF8,0x7C,0x3E,0x1F,0x0F,0x07,0x07,0x83,
	0xC1,0xF0,0xF8,0xFE,0x7C,0x3C,0x18,0x08,0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,
	0x0F,0x1F,0x1F,0x3E,0x7C,0xFC,0xF8,0xF0,0xF0,0x60,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x0F,0x0F,0x0F,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,
	0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0xC7,0xF7,0xFF,0xFF,0xFF,0x7F,0x37,
	0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,
	0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x0F,0x0F,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xF8,0xFF,0xFF,0x1F,0x07,
	0x01,0x00,0x00,0x00,0x00,0x01,0x01,0x81,0xC0,0xC0,0xE0,0xF0,0xF8,0xFC,0xFF,0xFF,
	0xDF,0x9F,0x1F,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x9C,0xFC,0xFC,0xFC,0xFE,
	0x3E,0x1E,0x06,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0xC0,0xE0,0xF0,0xFC,0xFE,0x3F,0x1F,0x0F,0x07,0x01,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x70,0xF0,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF8,0xFC,0xFF,0x7F,0x0F,0x03,0x00,0x00,0x00,
	0x00,0x00,0x00,0x02,0x06,0x0F,0x1F,0x0F,0x0F,0x07,0x03,0x01,0x00,0x01,0x03,0x07,
	0x0F,0x1F,0x3E,0x7C,0xF8,0xF0,0xE0,0xE0,0xF8,0xFC,0x7E,0x3F,0x1F,0x07,0x03,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF0,0xF8,
	0xFC,0x7E,0x3F,0x1F,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0F,0x1F,0x7F,0xFC,0xF8,0xF0,0xE0,
	0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xF0,0xF0,0xF8,0x78,
	0x7C,0x3C,0x3E,0x1F,0x1F,0x0F,0x07,0x07,0x07,0x0F,0x0F,0x1F,0x1E,0x3C,0x3C,0x78,
	0x78,0xF8,0xF0,0xF0,0xF0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x1C,0x7E,0x3F,0x3F,0x3F,0x3F,0x3F,0x3D,
	0x3C,0x3C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1E,0x1E,0x1E,0x0E,0x0E,0x0E,
	0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x06,0x07,0x07,0x0F,
	0x3F,0x7F,0xFE,0xF8,0x70,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x01,0x07,0x07,0x03,0x03,0x03,0x03,0x01,0x01,0x01,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x01,0x01,0x01,0x03,0x03,0x03,0x03,0x03,0x01,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

int show_bmp(int fd, unsigned char *buffer, unsigned int length);

int oled_clear(int fd);


int main (int argc, char **argv)
{
	int		rv = -1;
	char	dev_name[DEVNAME_LEN];
	int 	fd = 0;
	unsigned char	*buf;

	memset(dev_name, 0, sizeof(dev_name));
	snprintf(dev_name, sizeof(dev_name),"/dev/spi_oled");


	//打开文件
	fd = open(dev_name, O_RDWR, 0755);
	if(fd < 0)
	{
		printf("file %s open failure\n", dev_name);
		return -1;
	}
	printf("open fd :%s [%d] successful\n",dev_name, fd);

	buf = mmap(NULL,1024,PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);//内存映射，会调用驱动的mmap函数  
	if(NULL==buf)
	{
		printf("oled mmap failure\r\n");
		close(fd);
		return -1;
	}
	else
	{
		printf("oled mmap OK\n");
	}

	if(NULL == memcpy(buf,databuf,sizeof(databuf)))
	{
		printf("memcpy failure\n");
		return -1;
	}

	for(int i=0;i<1024;i++)
	{
		printf("%x",buf[i]);
	}
	printf("\n\n");

	read(fd, rx_buf,1024);
	for(int i=0;i<1024;i++)
	{
		printf("%x",rx_buf[i]);
	}

	rv = ioctl(fd,OLED_ON,1);
	if( rv==0 )
	{
		printf("ioctl OK\n");
	}
	else{
		printf("ioctl failure\n");
	}

	munmap(buf, 1024);

	sleep(30);

	//关闭文件
	rv  = close(fd);
	if(rv<0)
	{
		printf("close file error");
	}
	
	return 0;
} 

/* 清屏函数
 * *fd,打开文件描述符
 **/
int oled_clear(int fd)
{
	int rw = 0;
	int x,y;
	char buff[1024];

	memset(buff,0x00,sizeof(buff));

	//写入
	rw = write(fd, buff, sizeof(buff));
	if(rw < 0)
	{
		printf("write to buf error:%d\n",rw);
		return -1;
	}

	return 0;
}

/* 显示图片
 * *fd，打开文件描述符
 * *buffer,要显示的数据地址
 * *length,数据长度
 * */
int show_bmp(int fd,unsigned char *buffer,unsigned int length)
{
	int rp;

	rp = write(fd, buffer, length);
	if(rp < 0 )
	{
		printf("show_bmp error number write :%d\n",rp);
		return -2;
	}
	printf("show_bup OK number write:%d\n",rp);
	return 0;
}


